using System;
using System.Collections.Generic;

using log4net;

using GymManager.Exception;
using GymManager.Mechanism;
using GymManager.Persistence.DAO;
using GymManager.Persistence.VO;

namespace GymManager.Business
{
    /// <summary>
    /// Class responsible by providing services regarding expense type
    /// </summary>
    public class ExpenseTypeServices
    {
        // logger instance
        private ILog _logManager = LogManager.GetLogger(typeof(ExpenseTypeServices));

        /// <summary>
        /// Method responsible by getting information about expense type in according to a given description
        /// </summary>
        /// <param name="String">expense type's description</param>
        /// <returns>ExpenseType - user value object matching with given description</returns>
        /// <see cref="GymManager.Exception.BusinessException"/>
        public ExpenseType findByDescription(String description)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            ExpenseType expenseTypeResult = null;

            try
            {
                ExpenseTypeDAO expenseTypeDAO = new ExpenseTypeDAO();
                expenseTypeResult = expenseTypeDAO.findByDescription(description);
            }
            catch (PersistenceException)
            {
                throw new BusinessException();
            }            
            
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return expenseTypeResult;
        }

        /// <summary>
        /// Method responsible by getting all active expense types
        /// </summary>
        /// <returns>IList - list of expense type </returns>
        /// <see cref="GymManager.Exception.BusinessException"/>
        public IList<ExpenseType> findAllActive()
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            IList<ExpenseType> expenseTypeList = null;

            try
            {
                ExpenseTypeDAO expenseTypeDAO = new ExpenseTypeDAO();
                expenseTypeList = expenseTypeDAO.findAllActive();
            }
            catch (PersistenceException)
            {
                throw new BusinessException();
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return expenseTypeList;
        }

        /// <summary>
        /// Method responsible by adding a new expense type to database
        /// </summary>
        /// <param name="ExpenseType">expense type information to be saved</param>
        /// <see cref="GymManager.Exception.BusinessException"/>
        public void create(ExpenseType expenseTypeVO)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            try
            {
                // adding expense type to database
                ExpenseTypeDAO expenseTypeDAO = new ExpenseTypeDAO();
                expenseTypeDAO.create(expenseTypeVO);
            }
            catch (PersistenceException)
            {
                throw new BusinessException();
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }
        }

        /// <summary>
        /// Method responsible by updating an expense type to database
        /// </summary>
        /// <param name="ExpenseType">expense type information to be saved</param>
        /// <see cref="GymManager.Exception.BusinessException"/>
        public void update(ExpenseType expenseTypeVO)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            try
            {
                // updating expense type to database
                ExpenseTypeDAO expenseTypeDAO = new ExpenseTypeDAO();
                expenseTypeDAO.update(expenseTypeVO);
            }
            catch (PersistenceException)
            {
                throw new BusinessException();
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }
        }

        /// <summary>
        /// Method responsible by finding expense type using any of its properties
        /// </summary>
        /// <param name="ExpenseType">expense type filter vo</param>
        /// <returns>IList - list of expense type retrieved in according to filter</returns>
        /// <see cref="GymManager.Exception.BusinessException"/>
        public IList<ExpenseType> findByAny(ExpenseType expenseTypeVOFilter)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            IList<ExpenseType> expenseTypeList = null;

            try
            {
                // formating information of expense type VO filter
                if (!String.IsNullOrEmpty(expenseTypeVOFilter.Description))
                {
                    expenseTypeVOFilter.Description = "%" + expenseTypeVOFilter.Description + "%";
                }

                // getting information about expense type
                ExpenseTypeDAO expenseTypeDAO = new ExpenseTypeDAO();
                expenseTypeList = expenseTypeDAO.findByAny(expenseTypeVOFilter);
            }
            catch (PersistenceException)
            {
                throw new BusinessException();
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return expenseTypeList;
        }

        /// <summary>
        /// Method responsible by removing an expense type from database
        /// </summary>
        /// <param name="Int32">expense type identifier</param>
        /// <see cref="GymManager.Exception.BusinessException"/>
        public void remove(Int32 expenseTypeId)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            try
            {
                // removing expense type from database
                ExpenseTypeDAO expenseTypeDAO = new ExpenseTypeDAO();
                expenseTypeDAO.remove(expenseTypeId);
            }
            catch (PersistenceException)
            {
                throw new BusinessException();
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }
        }

    }
}
